package com.heliang.dataaccess.dao.impl;

import java.util.ArrayList;
import java.util.List;
import net.tsz.afinal.exception.DbException;
import android.content.Context;
import android.util.Log;

import com.heliang.common.FileLog;
import com.heliang.dataaccess.bean.VersionBean;
import com.heliang.dataaccess.dao.BaseDao;
import com.heliang.dataaccess.dao.VersionDao;
import com.heliang.util.Constants;
import com.hl.component.exception.DaoException;
import com.hl.component.exception.ErrorCode;
import com.hl.component.exception.handle.ExceptionHandle;

public class VersionBeanDaoImpl extends BaseDao implements VersionDao {

    public VersionBeanDaoImpl(Context context) {
        super(context);
        initVersion();
    }

    public List<VersionBean> findVersionByVerIds(int[] idArray) throws DaoException {
        StringBuffer strWhere = new StringBuffer();
        List<VersionBean> versionBeans = new ArrayList<VersionBean>();

        /* 执行根据IDS查询版本 */
        try {
            /* 构建条件语句 */
            strWhere.append(" ID IN ( ");
            for (int i = 0; i < idArray.length; i++) {
                if (i == idArray.length - 1) {
                    strWhere.append(idArray[i]);
                } else {
                    strWhere.append(idArray[i] + ",");
                }
            }
            strWhere.append(" ) ");

            versionBeans = finalDb.findAllByWhere(VersionBean.class, strWhere.toString());
            return versionBeans;
        } catch (DbException e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_QUERY_ERROR_004, "VersionBeanDaoImpl.findVersionByVerIds()" + "根据版本IDs查询版本信息失败！");
            return null;
        }
    }

    public VersionBean findVersionByVerId(int id) throws DaoException {

        VersionBean versionBean = new VersionBean();
        try {
            versionBean = finalDb.findById(id, VersionBean.class);
            versionBean = finalDb.findById(id, VersionBean.class);
            return versionBean;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_QUERY_ERROR_004, "VersionBeanDaoImpl.findVersionByVerId()" + "根据版本ID查询版本信息失败！");
            return null;
        }
    }

    public boolean updateVersions(VersionBean versionBean) throws DaoException {
        try {
            finalDb.update(versionBean);
            return true;
        } catch (DbException e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_UPDATE_ERROR_003, "VersionBeanDaoImpl.updateVersions()" + "根据版本ID跟新版本信息失败！");
            return false;
        }
    }

    public boolean saveVersionBeans(VersionBean versionBean) throws DaoException {
        try {
            finalDb.save(versionBean);
            return true;
        } catch (DbException e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_SAVE_ERROR_001, "VersionBeanDaoImpl.saveVersionBeans()" + "保存版本信息失败！");
            return false;
        }
    }

    public boolean delVersionByVerIds(Integer[] idArray) throws DaoException {
        try {
            StringBuffer strWhere = new StringBuffer();
            strWhere.append(" ID IN (");
            for (int i = 0; i < idArray.length; i++) {
                if (i == idArray.length - 1) {
                    strWhere.append(idArray[i]);
                } else {
                    strWhere.append(idArray[i] + ",");
                }
            }

            finalDb.deleteByWhere(VersionBean.class, strWhere.toString());
            return true;
        } catch (DbException e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_DELETE_ERROR_002, "VersionBeanDaoImpl.delVersionByVerIds()" + "根据IDS删除版本信息失败！");
            return false;
        }
    }

    @Override
    public List<VersionBean> findAll() throws DaoException {
        try {
            return finalDb.findAll(VersionBean.class);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

    //
    // @Override
    // public boolean saveVersions(List<VersionBean> versionbeans) throws
    // DaoException {
    // try {
    // for (VersionBean versionBean : versionbeans) {
    // finalDb.save(versionBean);
    // }
    // return true;
    // } catch (Exception e) {
    // ExceptionHandle.throwDaoException(e, ErrorCode.DAO_SAVE_ERROR_001,
    // "VersionBeanDaoImpl.saveVersions()" + "批量保存版本信息失败！");
    // return false;
    // }
    // }

    public void initVersion() {

        List<VersionBean> versionBeans = new ArrayList<VersionBean>();
        versionBeans = finalDb.findAll(VersionBean.class);
        if (versionBeans.size() > 0) {
            return;
        }

        VersionBean verInfoCat = new VersionBean(Constants.VER_INFO_CAT, 0, "t_table_infoCat", "");
        VersionBean verInfo = new VersionBean(Constants.VER_INFO, 0, "t_table_info", "");
        VersionBean verRecommendInfo = new VersionBean(Constants.VER_RECOMMEND_INFO, 0, "t_table_info", "");
        VersionBean verAdvertisement = new VersionBean(Constants.VER_ADVERTISEMENT, 0, "t_table_adv", "");
        VersionBean verMember = new VersionBean(Constants.VER_MEMBER, 0, "t_table_member", "");
        VersionBean verProductCat = new VersionBean(Constants.VER_PRODUCT_CAT, 0, "t_table_productCat", "");

        try {
            this.saveVersionBeans(verInfoCat);
            this.saveVersionBeans(verInfo);
            this.saveVersionBeans(verRecommendInfo);
            this.saveVersionBeans(verAdvertisement);
            this.saveVersionBeans(verMember);
            this.saveVersionBeans(verProductCat);
        } catch (DaoException e) {
            String errorMsg = "插入数据库版本信息失败";
            if (e.getCause() != null) {
                Log.e(Constants.TAG, e.getCause().getMessage() + errorMsg);
                FileLog.log(" DataBaseHelper.onCreate " + e.getCause().getMessage() + errorMsg);
            } else if (e.getMessage() != null) {
                Log.e(Constants.TAG, e.getMessage() + errorMsg);
                FileLog.log(" DataBaseHelper.onCreate " + e.getMessage() + errorMsg);
            }
        }

    }

}
